package io.geekidea.boot.shop.service.base.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.dto.CollectDto;
import io.geekidea.boot.shop.entity.Collect;
import io.geekidea.boot.shop.mapper.CollectMapper;
import io.geekidea.boot.shop.query.app.AppCollectQuery;
import io.geekidea.boot.shop.query.web.CollectQuery;
import io.geekidea.boot.shop.service.base.api.CollectService;
import io.geekidea.boot.shop.vo.app.AppCollectVo;
import io.geekidea.boot.shop.vo.web.CollectVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 收藏 服务实现类
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService {

    @Autowired
    private CollectMapper collectMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addCollect(CollectDto dto) {
        Collect collect = new Collect();
        BeanUtils.copyProperties(dto, collect);
        return save(collect);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateCollect(CollectDto dto) {
        Integer id = dto.getId();
        Collect collect = getById(id);
        if (collect == null) {
            throw new BusinessException("收藏不存在");
        }
        BeanUtils.copyProperties(dto, collect);
        return updateById(collect);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteCollect(Integer id) {
        return removeById(id);
    }

    @Override
    public CollectVo getCollectById(Integer id) {
        return collectMapper.getCollectById(id);
    }

    @Override
    public Paging<CollectVo> getCollectPage(CollectQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<CollectVo> list = collectMapper.getCollectPage(query);
        Paging<CollectVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppCollectVo getAppCollectById(Integer id) {
        return collectMapper.getAppCollectById(id);
    }

    @Override
    public Paging<AppCollectVo> getAppCollectPage(AppCollectQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppCollectVo> list = collectMapper.getAppCollectPage(query);
        Paging<AppCollectVo> paging = new Paging<>(list);
        return paging;
    }

}
